/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2016 All Rights Reserved.
 */
package com.alipay.study.web.index;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alipay.study.common.model.test.User;

/**
 * 首页对应的controller
 * @author wb-qlj205528
 * @version $Id: IndexController.java, v 0.1 2016年11月9日 上午10:46:28 wb-qlj205528 Exp $
 */
@Controller
@RequestMapping(value = "/index", method = { RequestMethod.GET, RequestMethod.POST })
public class IndexController {

    /**
     * @throws IOException 
     * @throws IllegalStateException 
     * 
     */
    @ApiOperation(value = "首页", notes = "返回index.html")
    @RequestMapping(value = "/index.htm")
    public String index(Model model) {
        System.out.println("----running  in  index-----");
        model.addAttribute("username", "zhangsan");
        return "/index/index";
    }

    /**
     * 
     * 
     * @param model
     */
    @RequestMapping("/getindex")
    @ResponseBody
    @ApiOperation(value = "测试首页@ResponseBody 使用", notes = "测试所用")
    public void getIndex(Model model) {

        System.out.println("Index getIndex() is running");

        model.addAttribute("username", "zhangsan");
        model.addAttribute("password", "zhangsan123456");
    }

    /**
     * 
     * 
     * @param model
     */
    @RequestMapping("/get")
    @ResponseBody
    @ApiOperation(value = "测试get方法", notes = "测试get")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "参数id", required = true, dataType = "java.lang.String"),
            @ApiImplicitParam(name = "username", value = "参数username", required = true, dataType = "java.lang.String"),
            @ApiImplicitParam(name = "user", value = "参数user", required = true, dataType = "com.alipay.study.common.model.test.User") })
    public String get(String id, String username, User user) {

        System.out.println("Index getIndex() is running");

        return "hello world";
    }

    /**
     * 
     * 
     * @param model
     */
    @RequestMapping("/get/{username}/{password}")
    @ResponseBody
    @ApiOperation(value = "测试rest风格", notes = "测试rest风格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "/get/{username}/{password} 其中的username", required = true, dataType = "java.lang.String"),
            @ApiImplicitParam(name = "password", value = "/get/{username}/{password} 其中的password", required = true, dataType = "java.lang.String") })
    public String getRest(@PathVariable String username, @PathVariable String password) {

        System.out.println("Index getIndex() is running " + username + ":" + password);

        return username + password;
    }

    /**
     * 
     * 
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/post")
    @ResponseBody
    @ApiOperation(value = "post测试", notes = "post测试")
    @ApiImplicitParam(name = "user", value = "参数user", required = true, dataType = "com.alipay.study.common.model.test.User")
    public Map<String, String> postData(User user) {
        Map<String, String> result = new HashMap<String, String>();
        result.put("userName", user.getUsername());
        result.put("password", user.getPassword());
        return result;
    }

    /**
     * 
     * 
     * @param username
     * @param password
     * @return
     */
    @RequestMapping("/post/{id}")
    @ResponseBody
    @ApiOperation(value = "post rest 测试", notes = "post rest 测试")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "/post/{id} 中的id", required = true, dataType = "java.lang.String"),
            @ApiImplicitParam(name = "user", value = "参数user", required = true, dataType = "com.alipay.study.common.model.test.User") })
    public Map<String, String> postRestData(@PathVariable String id, User user) {
        Map<String, String> result = new HashMap<String, String>();
        result.put("userName", user.getUsername());
        result.put("password", user.getPassword());
        result.put("id", user.getId());
        return result;
    }
}
